package com.yaowk.system.Interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.json.Json;
import com.jfinal.log.Log;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.DateKit;
import org.apache.shiro.SecurityUtils;

import java.util.Date;

/**
 * 操作日志拦截器
 *
 * @authc yaowk
 * 2017/4/25
 */
public class LogInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        log(inv);
        inv.invoke();
    }

    protected void log(Invocation inv) {
        Log log = Log.getLog(Constant.SYSTEM_MODULE);
        if (log.isInfoEnabled()) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(DateKit.toStr(new Date(), DateKit.timeFormat));
            stringBuffer.append("  ");
            stringBuffer.append(inv.getControllerKey());
            stringBuffer.append("/");
            stringBuffer.append(inv.getMethodName());
            stringBuffer.append("  参数：");
            stringBuffer.append(Json.getJson().toJson(inv.getController().getParaMap()));
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            stringBuffer.append(" 操作者Id： ");
            //stringBuffer.append(user.getId());
            stringBuffer.append(" 名称： ");
           // stringBuffer.append(user.getName());
            log.info(stringBuffer.toString());
        }
    }
}
